// AppointmentPage.cpp
#include "appointmentpage.h"
#include <QComboBox>
#include <QPushButton>
#include <QVBoxLayout>
#include <QLabel>
#include <QLineEdit>
#include <QMessageBox>
#include <QRegularExpressionValidator>

AppointmentPage::AppointmentPage(int currentID, QString role, QWidget *parent)
    : QWidget(parent), currentID(currentID), role(role) {

    // 设置QComboBox的样式，确保鼠标悬停时文字可见
    departmentComboBox = new QComboBox(this);
    departmentComboBox->setStyleSheet(
        "QComboBox {"
        "    background-color: #ffffff;"
        "    border: 1px solid #dcdcdc;"
        "    padding: 5px;"
        "    min-width: 200px;"
        "    font-size: 14px;"
        "}"
        "QComboBox::item {"
        "    background-color: #ffffff;"
        "    color: #000000;"
        "}"
        "QComboBox::item:hover {"
        "    background-color: #e0e0e0;"
        "    color: #000000;"
        "}"
        "QComboBox::item:selected {"
        "    background-color: #4CAF50;"
        "    color: white;"
        "}"
    );

    doctorComboBox = new QComboBox(this);
    doctorComboBox->setStyleSheet(
        "QComboBox {"
        "    background-color: #ffffff;"
        "    border: 1px solid #dcdcdc;"
        "    padding: 5px;"
        "    min-width: 200px;"
        "    font-size: 14px;"
        "}"
        "QComboBox::item {"
        "    background-color: #ffffff;"
        "    color: #000000;"
        "}"
        "QComboBox::item:hover {"
        "    background-color: #e0e0e0;"
        "    color: #000000;"
        "}"
        "QComboBox::item:selected {"
        "    background-color: #4CAF50;"
        "    color: white;"
        "}"
    );

    tcp_dep = new ClientTcp(DEPARTMENTS_DATA);
    QString sql_dep = "select * from Departments";
    tcp_dep->sendMsg(sql_dep);

    connect(tcp_dep->socket, &QTcpSocket::readyRead, [&]() {
        depData = tcp_dep->getDepartments();
        departmentComboBox->clear();
        for (const auto &department : depData) {
            QString departmentName = department.value("DepartmentName").toString();
            departmentComboBox->addItem(departmentName);
        }
        updateDoctorComboBox();
    });

    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    setStyleSheet("background-color: #f9f9f9;");

    QLabel *titleLabel = new QLabel("预约挂号", this);
    titleLabel->setStyleSheet("font-size: 24px; font-weight: bold; margin-bottom: 20px;");
    titleLabel->setAlignment(Qt::AlignCenter);
    mainLayout->addWidget(titleLabel);

    mainLayout->addWidget(new QLabel("选择科室:", this));
    mainLayout->addWidget(departmentComboBox);

    mainLayout->addWidget(new QLabel("选择医生:", this));
    mainLayout->addWidget(doctorComboBox);

    connect(departmentComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &AppointmentPage::updateDoctorComboBox);

    patientNameEdit = new QLineEdit(this);
    patientNameEdit->setPlaceholderText("请输入您的姓名");
    mainLayout->addWidget(new QLabel("患者姓名:", this));
    mainLayout->addWidget(patientNameEdit);

    patientPhoneEdit = new QLineEdit(this);
    patientPhoneEdit->setPlaceholderText("请输入您的电话");
    patientPhoneEdit->setValidator(new QRegularExpressionValidator(QRegularExpression("[0-9]{11}"), this));
    mainLayout->addWidget(new QLabel("患者电话:", this));
    mainLayout->addWidget(patientPhoneEdit);

    QPushButton *submitButton = new QPushButton("提交预约", this);
    submitButton->setStyleSheet("background-color: #4CAF50; color: white; font-weight: bold; padding: 10px;");
    connect(submitButton, &QPushButton::clicked, this, &AppointmentPage::submitAppointment);
    mainLayout->addWidget(submitButton, 0, Qt::AlignCenter);

    setLayout(mainLayout);
}

void AppointmentPage::updateDoctorComboBox() {
    QString selectedDepartment = departmentComboBox->currentText();
    QString sql_doc = QString("SELECT * FROM Doctors WHERE Department = '%1'").arg(selectedDepartment);
    tcp_doc = new ClientTcp(DOCTORS_DATA);
    tcp_doc->sendMsg(sql_doc);

    connect(tcp_doc->socket, &QTcpSocket::readyRead, [&]() {
        QList<QJsonObject> doctorsData = tcp_doc->getDoctorsData();
        doctorComboBox->clear();
        for (const auto &doctor : doctorsData) {
            QString doctorName = doctor.value("Name").toString();
            int doctorId = doctor.value("DoctorID").toInt();
            doctorComboBox->addItem(doctorName, QVariant(doctorId));
        }
    });
}

void AppointmentPage::submitAppointment() {
    QString department = departmentComboBox->currentText();
    QString doctor = doctorComboBox->currentText();
    QString name = patientNameEdit->text();
    QString phone = patientPhoneEdit->text();

    if (name.isEmpty() || phone.isEmpty()) {
        QMessageBox::warning(this, "输入错误", "请输入完整的姓名和电话！");
        return;
    }

    tcp_app = new ClientTcp(INSERT_DATA);
    QString sql = QString("INSERT INTO Appointments (PatientID, DoctorID, AppointmentDate, Status) VALUES (%1, %2, datetime('now'), 'Scheduled')")
                    .arg(this->currentID)
                    .arg(doctorComboBox->currentData().toInt());

    tcp_app->sendMsg(sql);

    QMessageBox::information(this, "预约成功",
                             QString("您已成功预约 %1 的 %2 医生。\n姓名: %3\n电话: %4")
                             .arg(department).arg(doctor).arg(name).arg(phone));
}
